<?php
require_once 'Session.php';
require_once 'MySQL.php';
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Login
 *
 * @author gurzaf
 */
class Login {
    private $_user;
    private $_pass;
    private $_mysql;
    private $_message;

    function __construct($_user, $_pass) {
        $this->_user = $_user;
        $this->_pass = $_pass;
        $this->_mysql = new MySQL("localhost", "root", "root", "Login");
    }

    public function get_user() {
        return $this->_user;
    }

    public function set_user($_user) {
        $this->_user = $_user;
    }

    public function get_access() {
        return $this->_access;
    }

    public function set_access($_access) {
        $this->_access = $_access;
    }

    public function get_mysql() {
        return $this->_mysql;
    }

    public function set_mysql($_mysql) {
        $this->_mysql = $_mysql;
    }

    public function get_message() {
        return $this->_message;
    }

    public function set_message($_message) {
        $this->_message = $_message;
    }

    public function identificarse(){
        
        if($this->_user == ""){
            $this->_message = "Usuario invalido";
            return 0;
        }else if($this->_pass == ""){
            $this->_message = "Clave invalida";
            return 0;
        }else{
            if($this->_mysql->conectarse()){
                $query = "SELECT clave FROM user WHERE nombre='$this->_user'";
                $resultado = $this->_mysql->consultar($query);
                if(!$resultado){
                    $this->_message = "Usuario no existe";
                    return 0;
                }
                if($resultado[0][0]==$this->_pass){
                    Session::registrarSesion($this->_user);
                    return 1;
                }else{
                    
                    $this->_message = "Clave Incorrecta";
                    return 0;
                }
                $this->_mysql->desconectarse();
            }else{
                $this->_message = $this->_mysql->get_message();
                
                return 0;
            }
            
        }
    }

}
